package com.scpii.api.controller.content;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.scpii.api.common.annoation.Pagination;
import com.scpii.api.common.base.BaseController;
import com.scpii.api.domain.content.Activities;
import com.scpii.api.domain.user.UserActions;
import com.scpii.api.domain.user.UserActions.ActionType;
import com.scpii.api.service.content.ActivitiesService;
import com.scpii.api.service.user.UserActionsService;
import com.scpii.api.util.PrincipalUtil;

@Controller
@RequestMapping("/content/activities")
public class ActivitiesController extends BaseController {

	@Resource
	private ActivitiesService activitiesService;

	@Resource
	private UserActionsService userActionsService;

	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	@Pagination
	public List<Activities> getActivities(Activities record) {
		record.setAppId(PrincipalUtil.getPrincipal().getAppId());
		return activitiesService.selectBySelective(record);
	}

	@PreAuthorize("hasRole('ROLE_USER')")
	@RequestMapping(value = "/user", method = RequestMethod.GET)
	@ResponseBody
	public List<Activities> getUserActivities(Activities record) {
		return activitiesService.selectUsersActivities();
	}

	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@RequestMapping(value = "/user/status/{targetId}", method = RequestMethod.GET)
	@ResponseBody
	public List<UserActions> getUserActivitiesStatus(@PathVariable Long targetId) {
		String[] actionTypes = new String[] {
				ActionType.ACTIVITIES_JOIN.getValue(),
				ActionType.ACTIVITIES_FAV.getValue() };
		return userActionsService.selectUserStatusByTargetAndActionTypes(
				PrincipalUtil.getPrincipal().getId(), targetId, actionTypes);
	}
}
